package top.lgd.controller.study;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lgd.base.LgdConstants;
import top.lgd.base.LgdResult;
import top.lgd.controller.LgdController;
import top.lgd.pojo.entity.study.Content;
import top.lgd.pojo.res.study.CatalogDurationRes;
import top.lgd.pojo.res.study.DayDurationLine;
import top.lgd.pojo.res.study.StudyOverviewRes;
import top.lgd.service.study.CatalogService;
import top.lgd.service.study.ContentService;
import top.lgd.service.study.OverviewService;
import top.lgd.utils.DateUtils;
import top.lgd.utils.NumberUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @author YYJ
 * @version 1.0
 * @since 2023/12/26 11:50
 */
@RestController
@RequestMapping("/admin/study")
public class StudyAdminController extends LgdController {

    @Autowired
    private ContentService contentService;
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private OverviewService overviewService;

    @PostMapping("/queryStudying")
    public LgdResult<List<Content>> queryStudying() {
        QueryWrapper<Content> queryWrapper = new QueryWrapper();
        queryWrapper.eq(LgdConstants.COLUMN_DATA_USER_ID, current().getId())
                .eq(LgdConstants.COLUMN_STATUS, LgdConstants.LONG_ZERO)
                .orderByDesc(LgdConstants.COLUMN_UPDATE_TIME)
                .last("limit 8");
        List<Content> contentList = contentService.list(queryWrapper);

        contentList.forEach(c -> {
            c.setCatalogName(catalogService.queryAllName(c.getCatalogId()));
            c.setCurrentProgress(NumberUtils.div(c.getDoneIndex(), c.getTargetIndex(), 2).multiply(new BigDecimal(100)));
            if (null != c.getStartDate()) {
                LocalDate lastStudyDate = null != c.getEndDate() ? c.getEndDate() : LocalDate.now();
                c.setStudyDays((int) DateUtils.between(c.getStartDate(), lastStudyDate, ChronoUnit.DAYS) + 1);
            }
        });
        return LgdResult.success(contentList);
    }

    /**
     * 查询学习总览
     *
     * @return
     */
    @PostMapping("/queryOverview")
    public LgdResult<StudyOverviewRes> queryOverview() {
        return LgdResult.success(overviewService.queryOverview(current().getId()));
    }

    /**
     * 查询学习总览
     *
     * @return
     */
    @PostMapping("/queryDurationList")
    public LgdResult<List<DayDurationLine>> queryDurationList() {
        return LgdResult.success(overviewService.queryDayDurationList(current().getId()));
    }

    /**
     * 查询学习总览
     *
     * @return
     */
    @PostMapping("/queryCatalogDurationList")
    public LgdResult<List<CatalogDurationRes>> queryCatalogDurationList() {
        return LgdResult.success(overviewService.queryCatalogDurationList(current().getId()));
    }
}
